package com.dinggao.crossgo.module.roadbook.activity;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.dinggao.crossgo.R;
import com.dinggao.crossgo.core.base.BaseActivity;
import com.dinggao.crossgo.core.constant.CommonConstant;
import com.dinggao.crossgo.core.manage.ActivtyManager;
import com.dinggao.crossgo.core.manage.ApiManager;
import com.dinggao.crossgo.core.observer.EventIntet;
import com.dinggao.crossgo.core.observer.EventMsg;
import com.dinggao.crossgo.core.paser.FastJsonUtils;
import com.dinggao.crossgo.core.util.GlideImageLoader;
import com.dinggao.crossgo.core.util.LogUtils;
import com.dinggao.crossgo.core.util.SharedPreferencesUtil;
import com.dinggao.crossgo.module.roadbook.data.RoadDetailsBean;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RoadBookPublishDetailsActivity extends BaseActivity {


    @BindView(R.id.details_banner)
    Banner banner;

    @BindView(R.id.details_content)
    TextView detailsContent;

    public static final String TAG = "RoadDetailsActivity";
    @BindView(R.id.detail_time)
    TextView detailTime;
    @BindView(R.id.detail_location)
    TextView detailLocation;
    @BindView(R.id.details_jd)
    TextView detailsJd;
    @BindView(R.id.details_ydq)
    TextView detailsYdq;
    @BindView(R.id.details_tcq)
    TextView detailsTcq;
    @BindView(R.id.details_jyz)
    TextView detailsJyz;
    @BindView(R.id.details_xsj)
    TextView detailsXsj;
    @BindView(R.id.iv_top_bar_left)
    ImageView ivTopBarLeft;
    @BindView(R.id.tv_top_bar_title)
    TextView tvTopBarTitle;
    @BindView(R.id.iv_top_bar_right)
    ImageView ivTopBarRight;

    private List<Uri> uriList;
    private ArrayList<String> imageList;
    private int journalId;
    private String accessToken;
    private RoadDetailsBean roadDetailsBean;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_details;
    }

    @Override
    protected void onInitialization(Bundle bundle) {

        tvTopBarTitle.setText("路书详情");
        ivTopBarRight.setImageDrawable(getResources().getDrawable(R.drawable.common_submenu));

        accessToken = SharedPreferencesUtil.getSharedPreferencesString(RoadBookPublishDetailsActivity.this, CommonConstant.USERINFO_SHREDPREFERENCE,
                CommonConstant.USRERINFO_SHRAREDPREFERCE_ACCESS_TOKEN);
        journalId = Integer.parseInt(getIntent().getStringExtra("journalId"));
        LogUtils.e("tag", "accessToken:" + accessToken);
        LogUtils.e("tag", "journalId:" + journalId);
        onNetWork(journalId);

    }

    private void onNetWork(int journalId) {
        OkGo.<String>get(ApiManager.ROADBOOKJOURNAL_GET)
                .headers("accessToken", accessToken)
                .tag(this)
                .params("bookJournalId", journalId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        //   LogUtils.e(TAG, response.body());

                        uriList = new ArrayList<>();
                        imageList = new ArrayList<>();
                        roadDetailsBean = FastJsonUtils.getObject(response.body().toString(), RoadDetailsBean.class);
                        detailTime.setText(timeslash(roadDetailsBean.getData().getCreateTime()));
                        detailsContent.setText(roadDetailsBean.getData().getContent());
                        detailLocation.setText(roadDetailsBean.getData().getLocation());

                        setTabTest(roadDetailsBean.getData().getTab());


                        List<RoadDetailsBean.DataBean.ImgsBean> imgs = roadDetailsBean.getData().getImgs();
                        if (imgs.size() > 0) {
                            for (int i = 0; i < imgs.size(); i++) {
                                uriList.add(Uri.parse(ApiManager.IMAG_URL + imgs.get(i).getImg()));
                                initBanner(uriList);

                                imageList.add(ApiManager.IMAG_URL + imgs.get(i).getImg());
                                LogUtils.e(TAG, imageList.get(i));
                            }

                        } else {

                            banner.setVisibility(View.GONE);

                        }


                    }
                });
    }

    //动态设置tab
    private void setTabTest(String tab) {

        if (tab.contains("景点")) {
            detailsJd.setVisibility(View.VISIBLE);
        }

        if (tab.contains("营地区")) {
            detailsYdq.setVisibility(View.VISIBLE);
        }

        if (tab.contains("停车区")) {
            detailsTcq.setVisibility(View.VISIBLE);
        }

        if (tab.contains("加油站")) {
            detailsJyz.setVisibility(View.VISIBLE);
        }
        if (tab.contains("洗手间")) {
            detailsXsj.setVisibility(View.VISIBLE);
        }


    }

    private String timeslash(long time) {
        SimpleDateFormat sdr = new SimpleDateFormat("MM/dd HH:mm");
        String times = sdr.format(new Date(time));
        return times;

    }


    private void initBanner(final List<Uri> uriList) {

        banner.setImages(uriList)
                .setImageLoader(new GlideImageLoader())
                .setDelayTime(3000)
                .start();

        banner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (imageList.size() == 1) {
                    Intent intent = new Intent(RoadBookPublishDetailsActivity.this, RoadBookPublishImageActivity.class);
                    intent.putStringArrayListExtra("imageurl", imageList);
                    intent.putExtra("position", 0);
                    startActivity(intent);
                }
            }
        });

        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                LogUtils.e(TAG, position + "");
                Intent intent = new Intent(RoadBookPublishDetailsActivity.this, RoadBookPublishImageActivity.class);

                intent.putStringArrayListExtra("imageurl", imageList);
                intent.putExtra("position", position);
                startActivity(intent);
            }
        });

    }


    @OnClick({R.id.iv_top_bar_left, R.id.iv_top_bar_right, R.id.details_content, R.id.detail_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_top_bar_left:
                EventIntet.getDefault().post(new EventMsg("RoadBookPublishDetailsActivity--black", 1, String.valueOf(getClass().getName())));
                finish();
                break;
            case R.id.iv_top_bar_right:
                showBottomDialog();
                break;

            case R.id.details_content:

                break;
            case R.id.detail_time:

                break;
        }
    }


    public void showBottomDialog() {
        final Dialog dialog = new Dialog(this);//可以在style中设定dialog的样式

        dialog.setContentView(R.layout.dialog_bottom);
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.gravity = Gravity.BOTTOM;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setAttributes(lp);
        //设置该属性，dialog可以铺满屏幕
        dialog.getWindow().setBackgroundDrawable(null);
        dialog.show();
        slideToUp(dialog.getWindow().findViewById(R.id.bootom_dialog_layout));

        TextView bianji = (TextView) dialog.findViewById(R.id.dialog_bianji);

        bianji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();

                Intent intent = new Intent(RoadBookPublishDetailsActivity.this, RoadBookPublishReleaseActivity.class);
                intent.putExtra("RoadBookPublishDetailsActivity", "BLACKdetails");
                intent.putExtra("RoadDetailsBean", roadDetailsBean);
                startActivity(intent);
                ActivtyManager.getAppManager().finishActivity(RoadBookPublishDetailsActivity.this);


            }
        });


        final TextView delete = (TextView) dialog.findViewById(R.id.dialog_delete);

        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                //"http://192.168.1.122:8080/yueye/roadBookJournal/delete/215"
                dialog.dismiss();

                OkGo.<String>delete(ApiManager.ROADBOOKJOURNAL_DELETE + journalId)
                        .headers("accessToken", accessToken)
                        .tag(this)
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(Response<String> response) {

                                LogUtils.e("delete", response.body());
                                EventIntet.getDefault().post(new EventMsg("RoadBookPublishDetailsActivity--delete", 9902, String.valueOf(getClass().getName())));
                                finish();

                            }

                            @Override
                            public void onError(Response<String> response) {
                                super.onError(response);
                                LogUtils.e("delete", "error" + response.body());

                            }
                        });

            }
        });

        TextView cancle = (TextView) dialog.findViewById(R.id.dialog_cancle);

        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog.dismiss();

            }
        });
    }

    /**
     * 弹出动画
     *
     * @param view
     */
    public static void slideToUp(View view) {
        Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                1.0f, Animation.RELATIVE_TO_SELF, 0.0f);

        slide.setDuration(500);
        slide.setFillAfter(true);
        slide.setFillEnabled(true);
        view.startAnimation(slide);

        slide.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


}
